#!/bin/bash
# coding: utf-8
# Orca symbols import
# Copyright (C) 2020-2023 Samuel Thibault <samuel.thibault@ens-lyon.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

export TEXTDOMAINDIR=/usr/local/share/locale

num=$1

LANG=C python3 > orca$num.autosym << EOF
from orca.chnames import chnames
from orca.punctuation_settings import punctuation, LEVEL_ALL, LEVEL_MOST, LEVEL_SOME, LEVEL_NONE, PUNCTUATION_REPLACE, PUNCTUATION_INSERT

LEVEL_CHAR = -1
l = {
  LEVEL_CHAR: 'char',
  LEVEL_ALL: 'all',
  LEVEL_MOST: 'most',
  LEVEL_SOME: 'some',
  LEVEL_NONE: 'none',
}

mypunctuation = {
  ' ': [ LEVEL_CHAR, PUNCTUATION_REPLACE ],
  '\\n': [ LEVEL_CHAR, PUNCTUATION_REPLACE ],
  '\\t': [ LEVEL_CHAR, PUNCTUATION_REPLACE ],
  '\\\\': [ LEVEL_MOST, PUNCTUATION_REPLACE ],
  '\\u00a0': [ LEVEL_CHAR, PUNCTUATION_REPLACE ],
  '\\u00a1': [ LEVEL_SOME, PUNCTUATION_REPLACE ],
  '\\u00a4': [ LEVEL_ALL, PUNCTUATION_REPLACE ],
  '\\u00a6': [ LEVEL_MOST, PUNCTUATION_REPLACE ],
  '\\u00a8': [ LEVEL_MOST, PUNCTUATION_REPLACE ],
  '\\u00aa': [ LEVEL_SOME, PUNCTUATION_REPLACE ],
  '\\u00ad': [ LEVEL_MOST, PUNCTUATION_REPLACE ],
  '\\u00af': [ LEVEL_MOST, PUNCTUATION_REPLACE ],
  '\\u00b4': [ LEVEL_MOST, PUNCTUATION_REPLACE ],
  '\\u00b5': [ LEVEL_SOME, PUNCTUATION_REPLACE ],
  '\\u00b6': [ LEVEL_MOST, PUNCTUATION_REPLACE ],
  '\\u00b8': [ LEVEL_MOST, PUNCTUATION_REPLACE ],
  '\\u00ba': [ LEVEL_SOME, PUNCTUATION_REPLACE ],
  '\\u00bf': [ LEVEL_SOME, PUNCTUATION_REPLACE ],
  '\\u0192': [ LEVEL_ALL, PUNCTUATION_REPLACE ],
  '\\u2034': [ LEVEL_SOME, PUNCTUATION_REPLACE ],
  '\\ufffc': [ LEVEL_NONE, PUNCTUATION_REPLACE ],
}

for sym,name in chnames.items():
    c = ord(sym[0])
    if ('$num' != '-chars') == \
        (c >= 0xc0 and c <= 0xff
         or c == 0x178
         or c == 0xe00a
         or c == 0xe00c):
        continue

    level = 'none'
    keep = ''

    if sym in punctuation:
        level, flag = punctuation[sym]
    elif sym in mypunctuation:
        level, flag = mypunctuation[sym]
    else:
        print("# %s not in punctuation" % sym)
        if '$num' == '-chars':
            level, flag = LEVEL_CHAR, PUNCTUATION_REPLACE
            keep = '\tnorep'
        else:
            level, flag = LEVEL_NONE, PUNCTUATION_REPLACE

    if flag == PUNCTUATION_INSERT:
        keep = '\talways'
    if level not in l:
        print("oops, %s not in l!?" % level)

    if sym == '\\t':
        sym = "\\\\t"
    elif sym == '\\n':
        sym = "\\\\n"
    elif sym == '\\\\':
        sym = "\\\\\\\\"
    elif sym == '#':
        sym = "\\\\#"

    print("%s\\t%s\\t%s%s" % (sym, name, l[level], keep))
EOF

for lang in base $(cat orca.polist)
do
	mkdir -p $lang
	(
		cat << EOF
# This file was automatically generated by make import-orca
# DO NOT MODIFY IT!
# See locale/README.md to know how to import dictionaries

# locale/$lang/orca.dic
# Copyright (C) 2004-2020 Free Software Foundation, Inc.
# This file is distributed under the same license as the orca package.

symbols:
EOF
		IFS=$'\t'
		cat orca$num.autosym | ( while read -r sym C_version level keep
		do
			CTX=""
			case "$sym" in
				\#*) continue ;;
			esac
			case "$C_version" in
				'') echo ; continue ;;
			esac
			case "$C_version" in
				math\ symbol*)
					CTX="--context=math symbol"
					C_version=${C_version#math symbol }
					;;
			esac

			sym="${sym//\\/\\\\}"
			sym="${sym//[/\\[}"
			if [ -n "$keep" ]
			then
				keep=$'\t'"$keep"
			fi

			if ! grep "^$sym	" base/symbols.dic > /dev/null 2>&1 && \
			   ! grep "^$sym	" $lang/symbols.dic > /dev/null 2>&1
			then
				translation=`LANGUAGE="$lang" gettext -d orca $CTX "$C_version"`

				if [ "$lang" == base -o "$translation" != "$C_version" ]
				then
					printf "%s\t%s\t$level$keep\n" "$sym" "$translation"
				fi
			fi
		done )
	) > $lang/orca$num.dic
	grep $'\t' $lang/orca$num.dic > /dev/null || rm -f $lang/orca$num.dic
done

rm -f orca$num.autosym
